#include "VLCWidget.h"
#include "ui_VLCWidget.h"

#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QDir>

//普通函数
static
void vlc_callback( const struct libvlc_event_t *p_event, void *p_data )
{
    qDebug() << endl << ">> vlc_callback " << endl;
    VLCWidget * pWidget = static_cast<VLCWidget*>(p_data);
    switch(p_event->type) {
    case  libvlc_MediaPlayerPositionChanged:
    {
        //播放器的位置发生改变时，要做的事儿： 1. 更新UI界面上进度条的位置
        //这意味着在该函数中必须要拿到VLCWidget对象
        int pos = libvlc_media_player_get_position(pWidget->mediaPlayer()) * 100;
        pWidget->setTimeSlider(pos);

        //播放器的位置发生改变时，要做的事儿：2. 更新时间的信息
        //a. 获取视频文件的总时长
        libvlc_time_t totalSec = pWidget->toalSecs();
        //b. 获取当前视频文件播放的时间
        libvlc_time_t curSec = libvlc_media_player_get_time(pWidget->mediaPlayer()) / 1000;
        int HH = totalSec / 3600;
        int MM = totalSec % 3600 / 60;
        int SS = totalSec % 60;
        int hh = curSec / 3600;
        int mm = curSec % 3600 / 60;
        int ss = curSec % 60;
        char buff[20] = {0};
        sprintf(buff, "%02d:%02d:%02d/%02d:%02d:%02d",
                hh, mm, ss, HH, MM, SS);
        QString timeStr(buff);
        pWidget->setTimeLabel(timeStr);
    }

    }//end of switch
}

VLCWidget::VLCWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VLCWidget)
{
    ui->setupUi(this);
    ui->timeLabel->clear();
}

VLCWidget::~VLCWidget()
{
    delete ui;
}

void VLCWidget::setTimeSlider(int value)
{
    qDebug() << " timeSlider value:" << value << endl;
    ui->timeSlider->setValue(value);
}

void VLCWidget::setTimeLabel(const QString &str)
{
    qDebug() << " timeLable:" << str << endl;
    ui->timeLabel->setText(str);
}

void VLCWidget::on_btnOpen_clicked()
{
    //通过文件的对话框查找一个视频文件
    QString filename = QFileDialog::getOpenFileName(this, "查找文件", "D:/videos/",
                                 "视频文件(*.mp4);;"
                                 "视频文件(*.avi);;"
                                 "所有文件(*.*)");
    qDebug() << "filename: " << filename << endl;

    //将Linux风格的路径信息转换为Windows风格的路径信息
    filename = QDir::toNativeSeparators(filename);
    qDebug() << ">> filename: " << filename << endl;

    //创建libvlc_instance_t的vlc实例
    _pInstance = libvlc_new(0, nullptr);
    if(!_pInstance) {
        QMessageBox::information(this, "提示", "libvlc_new failed");
        return ;
    }

    //创建媒体对象
    _pMedia = libvlc_media_new_path(_pInstance, filename.toStdString().c_str());
    if(!_pMedia) {
        QMessageBox::information(this, "提示", "libvlc_media_new_path failed");
        return ;
    }
    //获取媒体对象的播放总时长
    libvlc_media_parse(_pMedia);
    _totalSec = libvlc_media_get_duration(_pMedia) / 1000;



    //创建媒体播放器
    _pMediaPlayer = libvlc_media_player_new_from_media(_pMedia);
    if(!_pMediaPlayer) {
        QMessageBox::information(this, "提示", "libvlc_media_player_new_from_media failed");
        return ;
    }

    //设置播放窗口
    libvlc_media_player_set_hwnd(_pMediaPlayer, (void*)ui->videoWidget->winId());

    //创建媒体播放器的事件管理器
    _pEventManager = libvlc_media_player_event_manager(_pMediaPlayer);
    if(!_pEventManager) {
        QMessageBox::information(this, "提示", "libvlc_media_player_event_manager failed");
        return;
    }

    //注册事件及相应的事件处理器
    libvlc_event_attach(_pEventManager, libvlc_MediaPlayerPositionChanged, vlc_callback, this);

    //进行播放
    libvlc_media_player_play(_pMediaPlayer);
}

void VLCWidget::on_btnPlayer_clicked()
{
    libvlc_state_t st = libvlc_media_player_get_state(_pMediaPlayer);
    if(st == libvlc_Paused ||
       st == libvlc_Stopped) {
        libvlc_media_player_play(_pMediaPlayer);
    }
}

void VLCWidget::on_btnPause_clicked()
{
    libvlc_state_t st = libvlc_media_player_get_state(_pMediaPlayer);
    if(st == libvlc_Playing) {
        libvlc_media_player_pause(_pMediaPlayer);
    }
}

void VLCWidget::on_btnStop_clicked()
{
    libvlc_state_t st = libvlc_media_player_get_state(_pMediaPlayer);
    if(st == libvlc_Playing ||
       st == libvlc_Paused) {
        libvlc_media_player_stop(_pMediaPlayer);
    }
}
